package com.example.denglu.servlet;

import com.example.denglu.service.UserService;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;

@WebServlet("/registerByUsername")
public class UsernameRegisterServlet extends HttpServlet {
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        
        response.setContentType("application/json;charset=utf-8");
        
        // 简单的输入验证
        if (username == null || username.trim().isEmpty() || 
            password == null || password.trim().isEmpty()) {
            response.getWriter().write("{\"success\":false,\"message\":\"用户名和密码不能为空\"}");
            return;
        }
        
        // 注册用户
        boolean isSuccess = UserService.registerByUsername(username, password);
        
        response.getWriter().write("{\"success\":" + isSuccess + 
                ",\"message\":\"" + (isSuccess ? "注册成功" : "注册失败") + "\"}");
    }
} 